<?php
namespace Util\Str;
	function reMark($mark,$source){
		$temp = $source;
		for($i=0;$i<count($mark);$i++){	
			$temp=preg_replace("#\|\:mark\:".$mark[$i][0]."\:\|#",$mark[$i][1],$temp);
		}
		return $temp;
	}
		function getNoneHTMLContent($str){
			$temp=preg_replace("#\<#","&lt;",$str);
			$temp=preg_replace("#\>#","&gt;",$temp);
			return $temp;
		}
		function getHTMLContent($str){			
			return preg_replace("#\r\n|\n|\r\t#","<br/>",$str);
		}
		function getHTMLToSend($str){
			return	preg_replace("#(\\\|\'|\")#","\\\\$1",$str);
		} 
	class Mark{
		private $pattern;
		function __construct(){
			$this->pattern = array();
		}
		function addMark($title,$value){
			$this->pattern[] = array($title,$value);
			return count($this->pattern)-1;
		}
		function setValue($mark,$value){
			$this->pattern[$mark][1] = $value;
		}
		function reMark($source){
			$temp = $source;
			for($i=0;$i<count($this->pattern);$i++){	
				$temp=preg_replace("#\|\:mark\:".$this->pattern[$i][0]."\:\|#",$this->pattern[$i][1],$temp);
			}
			return $temp;
		}
		function reMarkEnd($source){
			$temp = $this->remark($source);
			return $this->clearmark($temp);
		}
		function clearMark($source){
			return preg_replace("#\|\:mark\:\w*\:\|#","",$source);
		}
	}
	class Pattern{
		private $pattern='';
		private $mark=Array();
		function __construct($source){
			$this->pattern = $source;
			$matches=Array();
			preg_match_all ('/\|\:lang\:(\w*[\d\w]*\.?[\w\d]*)\:([\w\s\pL]*)\:\|/u', $source ,&$matches);
			if(count($matches)>1){
				$num = count($matches[1]);
				require_once("lang.api.php");
				$l = new \Util\Lang\LangBridge();
				for($i=0;$i<$num;$i++){
					$this->pattern=str_replace($matches[0][$i],($l->get($matches[1][$i])?$l->get($matches[1][$i]):$matches[2][$i]),$this->pattern);
				}
			}
			preg_match_all ('/\|\:mark\:(\w*)\:\|/', $source ,&$matches);
			if(count($matches)>1){
				for($i=0;$i<count($matches[1]);$i++){
					if (!array_key_exists($matches[1][$i], $this->mark)) {
						$this->mark[$matches[1][$i]] = '';
					}
				}
			}
		}
		public function __set($name, $value){
			$matches=Array();
			preg_match_all ('/\|\:lang\:(\w*[\d\w]*\.?[\w\d]*)\:([\w\s\pL]*)\:\|/u', $value ,&$matches);
			if(count($matches)>1){
				$num = count($matches[1]);
				require_once("lang.api.php");
				$l = new \Util\Lang\LangBridge();
				for($i=0;$i<$num;$i++){
					$this->pattern=str_replace($matches[0][$i],($l->get($matches[1][$i])?$l->get($matches[1][$i]):$matches[2][$i]),$value);
				}
			}
			$this->mark[$name] = $value;
		}
		public function __get($name) {
			if (array_key_exists($name, $this->mark)) {
				return $this->mark[$name];
			}
			return null;
		}
		public function publish(){
			$rs='';
			if(func_num_args()==1){
				$rs =func_get_arg(0);
			}
			$temp = $this->pattern;$r='';
			if($rs instanceof databaseconnect\resurt){
				do{
					foreach($this->mark as $mark=>$markvalue){
						$nar = explode('_.',$markvalue);
						foreach($nar as $n){
							if($n[0]=="'"||$n[0]=='"'){
								$name .= substr($n,1,strlen($n)-2);
							}else{
								$name .= $rs->row[$n];
							}						
						}				
						$temp = preg_replace("#\|\:mark\:".$mark."\:\|#",$name,$temp);
						$name='';
					}
					$r.=$temp;
					$temp=$this->pattern;
				}while($rs->seek());
			}else{
				foreach($this->mark as $mark=>$markvalue){
					$nar = explode('_.',$markvalue);
					foreach($nar as $n){
						if($n[0]=="'"||$n[0]=='"'){
							$name .= substr($n,1,strlen($n)-2);
						}else{
							$name .= (isset ($$n))?$$n:$n;
						}						
					}				
					$temp = preg_replace("#\|\:mark\:".$mark."\:\|#",$name,$temp);
					$name='';
				}
				$r = $temp;
			}
			return $r;
		}
	}
	class PatternManager{
		private $Pattern = array();
		function __construct($ptnfile){
			if(file_exists($ptnfile)){
				$xmldoc = @simplexml_load_file($ptnfile);
				$xpath = "//Pattern";
				$value = $xmldoc->xpath($xpath);
				if(is_array($value)){
					foreach($value as $pattern){
						$name = $pattern->attributes();
						$name = (String)$name['title'];
						$content = (String)$pattern;
						if(!array_key_exists($name, $this->Pattern)){
							$this->Pattern[$name] = new Pattern($content);
						}
					}
				}
			}
		}
		public function __get($name) {
			if (array_key_exists($name, $this->Pattern)) {
				return $this->Pattern[$name];
			}
			return null;
		}
	}
?>